package com.oocl.MyHttpServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.oocl.MyHttpServer.controller.ServerController;
import com.oocl.MyHttpServer.controller.impl.ServerControllerImpl;
import com.oocl.MyHttpServer.runnable.ServerRunnable;

/**
 * Hello world!
 * 
 */
public class ManageServer implements Server {
	private ServerSocket server;
	private ServerController controller;
	
	public ManageServer(Server server) {
		this.controller = new ServerControllerImpl(server);
	}

	public void start() {
		try {
			server = new ServerSocket(9000);
			ExecutorService service = Executors.newSingleThreadExecutor();
			while (true) {
				Socket socket = server.accept();
				// /让一个单例线程去实现对浏览器的请求的处理
				service.submit(new ServerRunnable(socket,this.controller));
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		try {
			this.server.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public boolean isClosed() {
		return this.server==null || this.server.isClosed();
	}

}
